package top.anydata.lifeofcode.lifesystem.web.controller.admin.app;

import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import top.anydata.lifeofcode.lifesystem.dto.sys.app.SysAppDetailDto;
import top.anydata.lifeofcode.lifesystem.service.sys.app.ISysAppAdminService;
import top.anydata.lifeofcode.lifesystem.web.responses.admin.ApiResponse;

/**
 * 功能描述: 应用管理模块
 *
 * @author <a href="mailto:caingao@anydata.top">CainGao</a>
 * @version V_1.0
 * @date 2024/4/15 10:55 AM
 */
@Slf4j
@RestController
@RequestMapping("/sys/api/app")
@AllArgsConstructor
public class AppAdminController {

    @Autowired
    ISysAppAdminService sysAppAdminService;

    @GetMapping("/list")
    @PreAuthorize("hasPermission(#user,'sys::app::list')")
    public ResponseEntity<ApiResponse<Page<SysAppDetailDto>>> appList(int page, int size){
        return ResponseEntity.ok(
                ApiResponse.success(sysAppAdminService.listSysAppDetail(page, size))
        );
    }

    @GetMapping("/add")
    @PreAuthorize("hasPermission(#user,'sys::app::add')")
    public ResponseEntity<String> addApp(SysAppDetailDto sysAppDetailDto){
        log.info("add app:{}", sysAppDetailDto);
        sysAppAdminService.saveSysAppDetail(sysAppDetailDto);
        return ResponseEntity.ok("success");
    }

    @PostMapping("/update")
    @PreAuthorize("hasPermission(#user,'sys::app::update')")
    public ResponseEntity<String> updateApp(SysAppDetailDto sysAppDetailDto){
        log.info("update app:{}", sysAppDetailDto);
        sysAppAdminService.updateSysAppDetail(sysAppDetailDto);
        return ResponseEntity.ok("success");
    }

    @DeleteMapping("/{id}")
    @PreAuthorize("hasPermission(#user,'sys::app::delete')")
    public ResponseEntity<String> deleteApp(@PathVariable("id") Integer id){
        log.info("delete app:{}", id);
        sysAppAdminService.deleteSysAppDetail(id);
        return ResponseEntity.ok("success");
    }



}
